/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2024-12-25
 * Time:23:34
 */
public class Demo9 {
    public static void main(String[] args) throws InterruptedException {
        Thread t=new Thread(()->{

            //Thread类内部，有一个现成的标志位，可以用来判定当前的循环是否要结束
            while (!Thread.currentThread().isInterrupted()){
                System.out.println("线程工作中");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    //假装没听见，循环继续正常工作
                    throw new RuntimeException(e);
                    //加上一个break,让线程立即结束
                    //break;
                }
            }
        });
        t.start();
        Thread.sleep(5000);
        System.out.println("让t线程终止");
        t.interrupt();//终止线程
        t.interrupt();
    }
}
